﻿// Inner Fire 游戏引擎库
// Resource - 资源基类
//
// Copyright (c) 2025 by 尔西大帝. All rights reserved.
//
// 所属层级：应用层(4)
// 所有的资源都继承此基类
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2025-01-14

#pragma once

#include <EASTL/vector.h>
#include <dx/texture/dx_texture.h>
#include <memory>
#include <static_string.h>

namespace ifire::game {
class Assets;

class Resource {
public:
  Resource(Assets* manager, ikit::sstr name)
      : manager_(manager), id(getNextID()), name(name), is_loaded(false) {}
  Resource(const Resource& lhs) = delete;
  Resource& operator=(const Resource& lhs) = delete;
  ~Resource() = default;

  void SetLoaded() { is_loaded = true; }
  bool IsLoaded() const { return is_loaded; }
  // 对资源进行装配，处理各种引用
  virtual void Setup() {}

  // 处理引用
  template <typename T>
  static void Setup(eastl::vector<std::unique_ptr<T>>& list) {
    for (auto& e : list) {
      e->Setup();
    }
  }

  // 通过名称获得项目
  template <typename T>
  static T* GetByName(eastl::vector<std::unique_ptr<T>>& list, ikit::sstr name,
      bool use_ready = false, T* default_item = nullptr) {
    if (name.empty()) return default_item;

    for (auto& e : list) {
      if (e->name == name) {
        if (use_ready){
          e->Ready();
        }
        return e.get();
      }
    }

    return default_item;
  }

  static uint64_t getNextID() {
    static uint64_t id = 0; // 静态局部变量，初始化为0
    return id++;            // 返回当前值后步进1
  }

public:
  // 资源的名称
  ikit::sstr name;
  // 资源的ID
  std::uint64_t id;

protected:
  // 父级
  Assets* manager_;
  // 是否已经载入
  bool is_loaded;
};

} // namespace ifire::game